//index.js
//获取应用实例
const app = getApp()
var value1 = '0';
var value2;
var value3 = '0';
var value4 = '0';
var value5 = '0';
var value6;
var limit_num = 0;

Page({
  data: {
    motto1: '0',
    motto2: '0',
    motto3: '0',
    motto4: '0',
    motto5: '0',
    motto6: '0',
    motto7: '0',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    switch1Checked: false,
    switch2Checked: false,
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }


    setInterval(function () {
      this.get_access_token();
      this.get_user_message();
      this.setData({
        motto1: value1,
        motto_userimage: value2,
        motto_level: value3,
        motto_nickname: value4,
        motto_uid: value5,
        motto_score:value6,
      })
    }.bind(this), 1000);
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },







  // test: function () {
  //   wx.request({
  //     url: 'https://api.heclouds.com/devices/523664158/datapoints',
  //     header: {
  //       "api-key": 'LGRkpye5=XLdBaS=9xT9K7TWMhw='
  //     },

  //     data: {
  //       "limit": (limit_num + 1)
  //     },
  //     method: "GET",
  //     success: function (res) {
  //       console.log(res.data.data);
  //       value1 = res.data.data.datastreams[5].datapoints[limit_num].value;
  //       value2 = res.data.data.datastreams[1].datapoints[limit_num].value;
  //       value3 = res.data.data.datastreams[0].datapoints[limit_num].at;
  //       value1 = value1.toString();
  //       value2 = value2.toString();
  //       value3 = value3.toString();
  //     },
  //   })
  // },
  //获取授权码(access_token)
  get_access_token: function () {
    wx.request({
      url: 'https://www.bigiot.net/oauth/token?grant_type=password&client_id=510&client_secret=0e8402e9ad&username=6599&password=b94b323e6b',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      success: function (res) {
        console.log(res.data);
        value1=res.data.access_token;
        console.log(value1);
      },
    })
  },
  get_user_message: function () {
    wx.request({
      url: 'https://www.bigiot.net/oauth/userinfo',
      header: {
        "Content-Type": "application/json"
      },
      data: {
        access_token: value1,//授权凭证,凭证内部已包含用户ID信息
      },
      method: "GET",
      success: function (res) {
        console.log(res.data);
        value2=res.data.image;
        value3 = res.data.level;
        value4 = res.data.nickname;
        value5 = res.data.uid;
        value6 = res.data.score;
      },
    })
  },
  // send3: function () {
  //   wx.request({
  //     url: 'https://api.heclouds.com/devices/523664158/datapoints',
  //     header: {
  //       "api-key": 'LGRkpye5=XLdBaS=9xT9K7TWMhw='
  //     },
  //     data: {
  //       "datastreams": [{ "id": "FAN", "datapoints": [{ "value": FAN }] }],
  //     },
  //     method: "POST",
  //     success: function (res) {
  //       console.log(res.data.data);
  //     },
  //   })
  // },
  switch1Change: function (e) {
    console.log(`Switch样式点击后是否选中：`, e.detail.value)

    if (e.detail.value) {
      LIGHT = 1;
      this.send();

    }
    else {
      LIGHT = 0;
      this.send();
    }
  },
  switch2Change: function (e) {
    console.log(`Switch样式点击后是否选中：`, e.detail.value)

    if (e.detail.value) {
      Servo = 1;
      this.send2();

    }
    else {
      Servo = 0;
      this.send2();
    }
  },
  switch3Change: function (e) {
    console.log(`Switch样式点击后是否选中：`, e.detail.value)

    if (e.detail.value) {
      FAN = 1;
      this.send3();

    }
    else {
      FAN = 0;
      this.send3();
    }
  },






})
